package cn.yusei.teamTribe.controller.user;

import java.io.PrintWriter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.service.user.PhoneSmsService;
import cn.yusei.teamTribe.util.PhoneUtil;
import cn.yusei.teamTribe.util.RandomCodeUtil;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class PhoneSmsController {
	@Autowired
	PhoneSmsService phoneSmsService;
	
	@RequestMapping(value = "/user/authPhone/get")
	public void getSms(@RequestParam("phone")String phone, PrintWriter out){
		//TODO 或许还要做一些验证，防止恶意请求
		if(!PhoneUtil.isPhoneNumber(phone)){
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try {
			code = phoneSmsService.getSms(phone, RandomCodeUtil.getCode());
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
	
	

}
